package com.aaa.dem1101.SafeLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/10/31  17:03
 */
public class MyTicket implements Runnable{
    private int ticket=100;
    private static Lock lock=new ReentrantLock();
    @Override
    public void run() {
        while (true){
            lock.lock();
            try {
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName()+"卖票一张,剩余："+ --ticket);
                }else {
                    break;
                }
            }finally {
                lock.unlock();
            }
        }
    }
}
